package com.example.distribued.lock;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.sql.Types;
import java.util.Collections;

/**
 * @author ash
 * @description
 * @create 2024/4/5  15:05:27
 * @className Generator
 * @verson 1.0.0
 */
@SpringBootTest
public class Generator {


    @Test
    public void  test(){
        FastAutoGenerator.create("jdbc:mysql://192.168.2.45:3306/?distributed", "root", "123")
                .globalConfig(builder -> {
                    builder.author("ash") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir("F://BaiduNetdiskDownload"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.example.distribued.lock") // 设置父包名
                            .moduleName("system") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "F://BaiduNetdiskDownload")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("db_lock") // 设置需要生成的表名
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .controllerBuilder()
                            .mapperBuilder();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
